package org.openatom.ubml.model.vo.definition.common;

import org.openatom.ubml.model.common.definition.cef.element.GspElementDataType;
import org.openatom.ubml.model.common.definition.cef.element.GspElementObjectType;
import org.openatom.ubml.model.common.definition.cef.util.Guid;
import org.openatom.ubml.model.common.definition.commonmodel.entity.object.GspColumnGenerate;
import org.openatom.ubml.model.vo.definition.GspViewModel;
import org.openatom.ubml.model.vo.definition.GspViewModelElement;
import org.openatom.ubml.model.vo.definition.GspViewObject;

/**
 * The Tool Of  View  Model Init
 *
 * @ClassName: InitVoUtil
 * @Author: Benjamin Gong
 * @Date: 2021/1/11 17:13
 * @Version: V1.0
 */
public final class InitVoUtil {
    public static GspViewModel BuildVirtualVo(String metadataID, String metadataName, String metadataCode, String metadataAssembly) {
        GspViewModel vo = InitVo(metadataID, metadataName, metadataCode, metadataAssembly, true);
        return vo;
    }

    private static GspViewModel InitVo(String metadataID, String metadataName, String metadataCode, String metadataAssembly, boolean isVirtual) {
        GspViewModel vo = new GspViewModel();
        vo.setID(metadataID);
        vo.setCode(metadataCode);
        vo.setName(metadataName);
        vo.setGeneratingAssembly(metadataAssembly);
        vo.setIsUseNamespaceConfig(true);
        GspViewObject mainObject = InitMainObject(metadataName, metadataCode, isVirtual);
        vo.setMainObject(mainObject);
        // vo
        if (isVirtual) {
            vo.setIsVirtual(true);
            vo.setMapping(null);
        }
        return vo;
    }

    private static GspViewObject InitMainObject(String objName, String objCode, boolean isVirtual) {
        GspViewObject mainObj = new GspViewObject();
        GspViewModelElement idElement = GetIDElement(isVirtual);
        mainObj.setID(Guid.newGuid().toString());
        mainObj.setCode(objCode);
        mainObj.setName(objName);
        GspColumnGenerate columnGenerate=new GspColumnGenerate();
        columnGenerate.setElementID(idElement.getID());
        columnGenerate.setGenerateType("Guid");
        mainObj.setColumnGenerateID(columnGenerate);
        mainObj.getContainElements().add(idElement);
        // voObject
        if (isVirtual) {
            mainObj.setIsVirtual(true);
            mainObj.setMapping(null);
        }
        return mainObj;
    }

    private static GspViewModelElement GetIDElement(boolean isVirtual) {
        GspViewModelElement idElement = new GspViewModelElement();
        idElement.setID(Guid.newGuid().toString());
        idElement.setCode("ID");
        idElement.setName("ID");
        idElement.setLabelID("ID");
        idElement.setMDataType(GspElementDataType.String);
        idElement.setObjectType(GspElementObjectType.None);
        idElement.setLength(36);
        idElement.setPrecision(0);
        idElement.setIsRequire(true);
        // voElemet
        if (isVirtual) {
            idElement.setIsVirtual(true);
            idElement.setIsVirtualViewElement(true);
            idElement.setMapping(null);
        }
        return idElement;
    }

}